//
//  OhterActiviyListTableViewController.m
//  app001
//
//  Created by xionghao on 15/10/14.
//  Copyright © 2015年 xionghao. All rights reserved.
//

#import "OhterActiviyListTableViewController.h"
#import "ZTActivityTableViewCell.h"
#import "PostActivityTableViewController.h"
#import <MJRefresh.h>
#import "Puser.h"
#import "ZTActivityModel.h"
#import "ZTListContent.h"
#import "UserInfoUpdateResde.h"
#import "MYActivityDetailViewController.h"
@interface OhterActiviyListTableViewController ()
@property (nonatomic,strong)  Puser *user;
@property (nonatomic,strong) NSMutableArray *datalist;
@end

@implementation OhterActiviyListTableViewController
-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}
-(NSMutableArray *)datalist{
    if ( _datalist==nil) {
        _datalist=[NSMutableArray array];
    }
    return  _datalist;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor=bgGarycolor;
    self.tableView.contentInset=UIEdgeInsetsMake(0, 0, 46, 0);
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight=220;
    [self setuptableView];
    self.navigationItem.title=self.isGet?@"交车纪念":@"营销活动";
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tableView.header beginRefreshing];
}
-(void)setuptableView{
    MJRefreshNormalHeader *header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.header=header;
    self.tableView.header.automaticallyChangeAlpha=YES;
    MJRefreshBackNormalFooter *footer=[MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadmoredata)];
    
    self.tableView.footer=footer;
    
    self.tableView.header=header;
    
}
-(void)loadmoredata{
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    if (self.isGet) {
        params[@"action"]=@"my_mark";
    }else{
        params[@"action"]=@"my_activity";
    }
    params[@"fabu"]=@"0";
    params[@"num"]=Kpagesize;

    
    params[@"user_id"]=self.user_id;
    params[@"type"]=self.user_type;
    NSMutableArray *IDarray=[NSMutableArray array];
    
    for (ZTActivityModel *mode in self.datalist) {
        [IDarray addObject:mode.ID];
    }
    
    params[@"page_id"]=[NSString DataTOjsonString:IDarray];
    
    
    [PostUitls Post:mynewzturl parameters:params success:^(id response) {
        
        NSDictionary *dict=response;
        [ZTActivityModel setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{@"ID":@"id"};
        }];
        [ZTActivityModel setupObjectClassInArray:^NSDictionary *{
            return @{@"content":@"ZTListContent"};
        }];
        
        NSArray *data=[ZTActivityModel objectArrayWithKeyValuesArray:dict[@"data"]];
        [self.datalist addObjectsFromArray:data];
        if (data.count<[Kpagesize integerValue]) {
            [self.tableView.footer noticeNoMoreData];
        }else{
            [self.tableView.footer resetNoMoreData];
        }
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
        
        
        
    } failure:^(NSError *error) {
        [self.tableView.footer endRefreshing];
    }];
}
-(void)refresh{
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    if (self.isGet) {
        params[@"action"]=@"my_mark";
    }else{
        params[@"action"]=@"my_activity";
    }
    params[@"fabu"]=@"0";
    params[@"num"]=Kpagesize;

    params[@"user_id"]=self.user_id;
    params[@"type"]=self.user_type;
    
    [PostUitls Post:mynewzturl parameters:params success:^(id response) {
        [self.datalist removeAllObjects];
        NSDictionary *dict=response;
        [ZTActivityModel setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{@"ID":@"id"};
        }];
        [ZTActivityModel setupObjectClassInArray:^NSDictionary *{
            return @{@"content":@"ZTListContent"};
        }];
        
        NSArray *data=[ZTActivityModel objectArrayWithKeyValuesArray:dict[@"data"]];
        if (data.count<[Kpagesize integerValue]) {
            [self.tableView.footer noticeNoMoreData];
        }else{
            [self.tableView.footer resetNoMoreData];
        }
        [self.datalist addObjectsFromArray:data];
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        
        
        
    } failure:^(NSError *error) {
        [self.tableView.header endRefreshing];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.datalist.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZTActivityTableViewCell *cell=[ZTActivityTableViewCell cellWithTableView:tableView];
    cell.hasspace=YES;
    // Configure the cell...
    ZTActivityModel *mode=self.datalist[indexPath.row];
    cell.mode=mode;
    cell.isEidt=NO;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZTActivityModel *mode=self.datalist[indexPath.row];
    MYActivityDetailViewController *vc=[[MYActivityDetailViewController alloc]init];
    vc.mode=mode;
    vc.isActiviy=!self.isGet;
    vc.isFromOtherZT=YES;
    [self.navigationController pushViewController:vc animated:YES];

}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
